//
//  WBMainController.swift
//  weibo
//
//  Created by 李江波 on 16/10/6.
//  Copyright © 2016年 lijiangbo. All rights reserved.
//

import UIKit

class WBMainController: UITabBarController {
    // MARK : - 私有控件 中间按钮
   fileprivate lazy var addButton = UIButton(imageName: "tabbar_compose_icon_add", bgImageName: "tabbar_compose_button")

    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
        
        setupButton()
        
    }
    
    /// 代码控制横竖屏 portrait :竖屏    landscape：横屏 只有当前控制器和子控制器准守
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
    
         return .portrait
    
    }

    
    

}

// MARK: - 中间按钮点击事件
extension WBMainController {
    
    @objc private func addBtnClick() -> () {
        
        print("点我了  啊啊啊")
        
        let vc = UIViewController()
        
        vc.view.backgroundColor = UIColor(alpha: 1)
        let nav = UINavigationController(rootViewController: vc)
        
        present(nav, animated: true, completion: nil) //可以查看横竖屏
        
        
    }
    
    
     /// 添加中间按钮
    fileprivate func setupButton() {
        
        tabBar.addSubview(addButton)
        
//        addButton.center = CGPoint(x: tabBar.center.x, y: tabBar.bounds.height * 0.5)
        
        let count = CGFloat(childViewControllers.count)
        let width = tabBar.bounds.width / count - 1 //盖住容错点，防止穿帮
        
        //insetBy 正数向内缩进，负数向外延伸
        addButton.frame = tabBar.bounds.insetBy(dx: width * 2, dy: 0)
        
        addButton.addTarget(self, action: #selector(addBtnClick), for: .touchUpInside)
    }

    fileprivate func setupUI() {
        
        //获取沙盒路径
        let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
        
        let jsonPath = (path as NSString).appendingPathComponent("main.json")
        
        var data = NSData(contentsOfFile: jsonPath)
        
        //判断沙盒有没有json数据
        if data == nil {
            
            let filePath = Bundle.main.path(forResource: "main.json", ofType: nil)
            data = NSData(contentsOfFile: filePath!)
            
        }
        /// 从bundle加载数组
        guard let array = try? JSONSerialization.jsonObject(with: data as! Data, options: []) as? [[String:Any]]
        
        else {
            return
        }
        
        var arrayM = [UIViewController]()
        
        for dict in array! {
            
            arrayM.append(setupChlidViewController(dict: dict))
        }
        
      viewControllers = arrayM
    }
    
    private func setupChlidViewController(dict:[String : Any]) -> UIViewController {
        
        guard let className = dict["className"] as? String,
        let title = dict["title"] as? String,
        let imageName = dict["imageName"] as? String,
            //根据名字创建类名，加上命名空间
        let clz = NSClassFromString(Bundle.main.namespace + "." + className) as? WBBaseController.Type,
            
        let visitorDict = dict["visitorInfo"] as? [String : String]
        
        else {
            
            return UIViewController()
        }
        
        let vc = clz.init()
        
        vc.visitorBaceInfo = visitorDict
        
        vc.title = title;
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")?.withRenderingMode(.alwaysOriginal)
        
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orange], for: .highlighted)
        
        let nav = WBNavController(rootViewController: vc)
        
        return nav
    }

}
